import 'dart:io';

import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path_provider/path_provider.dart';

class YZAliyunImage {
  /// bmiddle 取阿里云oss  w:360 (列表中的缩略图)
  static String bmiddleImage(String? url, {String? placeholder}) {
    var placeholderUrl = placeholder ?? '';

    var str = url == null
        ? placeholderUrl
        : url.isEmpty
            ? placeholderUrl
            : _ossResizeUrl(url, 360);

    return str;
  }

  /// large 取阿里云oss  w:480 (列表中的大图)
  static String middlePlusImage(String? url, {String? placeholder}) {
    var placeholderUrl = placeholder ?? '';

    var str = url == null
        ? placeholderUrl
        : url.isEmpty
            ? placeholderUrl
            : _ossResizeUrl(url, 480);

    return str;
  }

  static _ossResizeUrl(String url, int width) {
    return "$url?&x-oss-process=image/resize,m_fixed,w_$width";
  }

  /// compress file and get file.
  static void compressAndGetFile(
      File? file, void Function(String, String) resultFunc) async {
    if (file == null) {
      return null;
    }
    //获取临时目录
    var tempDir = await getTemporaryDirectory();
    var tempFileName = "image_${DateTime.now().millisecond}.jpg";
    //生成file文件格式
    var tempFile = await File('${tempDir.path}/$tempFileName').create();
    var result = await FlutterImageCompress.compressAndGetFile(
      file.absolute.path,
      tempFile.absolute.path,
      quality: 88,
    );

    // print(file.lengthSync());
    // print(result?.lengthSync());

    resultFunc(result?.path ?? "", tempFileName);
  }
}
